package com.aijk.web.admin.controller.sys;

import com.aijk.commons.exception.ErrorCodeException;
import com.aijk.commons.lang.utils.StringUtil;
import com.aijk.commons.utils.Utils;
import com.aijk.ehm.api.SysAppVersionApi;
import com.aijk.ehm.model.SysAppVersion;
import com.aijk.ehm.model.qo.SysAppVersionQo;
import com.aijk.web.admin.RetMsg;
import com.aijk.web.admin.utils.DataTableUtil;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;


@Controller
@RequestMapping(value = "/admin/appVersion")
public class SysAppVersionController {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SysAppVersionApi sysAppVersionApi;


    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list_view() {
        return "sys/version_list";
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg list(@RequestParam HashMap skey,@RequestParam(value = "draw", defaultValue = "1") Integer draw) {
        Long iTotalRecords;
        DataTableUtil.FormatOptions(skey);
        if (Utils.isNotEmpty(skey.get("orderbyclause"))) {
            skey.put("orderbyclause", StringUtil.toUnderlineCase(Utils.toString(skey.get("orderbyclause"))));
        }
        SysAppVersionQo qo = DataTableUtil.fillQo(skey, SysAppVersionQo.class);
        draw += 1;
        PageInfo<SysAppVersion> sysAppVersionPageInfo = sysAppVersionApi.page(qo);

        iTotalRecords =sysAppVersionPageInfo.getTotal();
        RetMsg regMsg = new RetMsg();

        regMsg.put("data", sysAppVersionPageInfo.getList());
        regMsg.put("draw", draw);
        regMsg.put("recordsTotal", iTotalRecords);
        regMsg.put("recordsFiltered", iTotalRecords);

        return regMsg;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Model model, Long id) {
        SysAppVersion bean = new SysAppVersion();
        if (id != null){
            bean = sysAppVersionApi.findOne(id);
        }
        model.addAttribute("sysAppVersion", bean);
        return "sys/version_edit";
    }


    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg save(SysAppVersion bean) {
        try {
            Long id = bean.getId();
            //SysAdmin admin = getCurrentUser();
            if (id == null) {// 添加
                sysAppVersionApi.save(bean);
            } else {
                SysAppVersion sysAppVersion = sysAppVersionApi.findOne(id);
                sysAppVersionApi.update(bean);
            }
        }catch (ErrorCodeException e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return RetMsg.failed();
        }
        return RetMsg.success();
    }





    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg deleteById(@RequestParam(value = "id") Long id) {
        try {
            sysAppVersionApi.deleteById(id);
        }catch (ErrorCodeException e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return RetMsg.failed();
        }
        return RetMsg.success();
    }







}
